<template>
  <div class="container-home__header">
    <el-button v-if="isEdit" @click="cancleDrag">取消</el-button>
    <el-button v-if="!isEdit" @click="isEdit = !isEdit" type="primary">编辑</el-button>
    <el-button v-if="isEdit" @click="isEdit = !isEdit" type="primary">保存</el-button>
  </div>
  <!-- <el-affix :offset="120" style="right: 20px">
    <el-button type="primary">Offset top 120px</el-button>
  </el-affix> -->
  <div class="container-home">
    <!-- <div>
      <div class="layoutJSON">
        Displayed as <code>[x, y, w, h]</code>:
        <div class="columns">
          <div class="layoutItem" v-for="item in layout">
            <b>{{ item.i }}</b
            >: [{{ item.x }}, {{ item.y }}, {{ item.w }}, {{ item.h }}]
          </div>
        </div>
      </div>
    </div>
    <br /> -->
    <div id="content" class="container-home__grids">
      <grid-layout
        ref="gridlayout"
        :layout.sync="layout"
        :col-num="12"
        :row-height="30"
        :is-draggable="true"
        :is-resizable="true"
        :vertical-compact="true"
        :use-css-transforms="true"
        @layout-updated="layoutUpdatedEvent"
      >
        <grid-item :key="item.i" v-for="item in layout" :x="item.x" :y="item.y" :w="item.w" :h="item.h" :i="item.i">
          <span v-if="item.i != 'drop'" class="text">{{ item.i }}</span>
        </grid-item>
      </grid-layout>
    </div>
    <div v-if="isEdit" class="container-home__drags">
      <template v-for="item in dragList" :key="item">
        <div @drag="drag(item)" @dragend="dragend(item)" class="droppable-element" draggable="true" unselectable="on">
          {{ item.w }}- {{ item.h }}
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { GridLayout, GridItem } from 'vue-grid-layout'

let mouseXY = { x: null, y: null }
let DragPos = { x: null, y: null, w: 1, h: 1, i: null }

export default {
  components: {
    GridLayout,
    GridItem,
  },
  data() {
    return {
      isEdit: false,
      layout: [
        { x: 0, y: 0, w: 2, h: 2, i: '0' },
        { x: 2, y: 0, w: 2, h: 4, i: '1' },
        { x: 4, y: 0, w: 2, h: 5, i: '2' },
        { x: 6, y: 0, w: 2, h: 3, i: '3' },
        { x: 8, y: 0, w: 2, h: 3, i: '4' },
        { x: 10, y: 0, w: 2, h: 3, i: '5' },
        { x: 0, y: 5, w: 2, h: 5, i: '6' },
        { x: 2, y: 5, w: 2, h: 5, i: '7' },
        { x: 4, y: 5, w: 2, h: 5, i: '8' },
        // { x: 5, y: 10, w: 4, h: 3, i: '9' },
      ],
      dragList: [
        { w: 1, h: 1, i: 'drop-0' },
        { w: 3, h: 3, i: 'drop-1' },
        { w: 6, h: 3, i: 'drop-2' },
        { w: 12, h: 4, i: 'drop-3' },
        { w: 8, h: 4, i: 'drop-4' },
        { w: 4, h: 4, i: 'drop-5' },
      ],
    }
  },
  mounted() {
    document.addEventListener(
      'dragover',
      function (e) {
        mouseXY.x = e.clientX
        mouseXY.y = e.clientY
      },
      false
    )
  },
  methods: {
    drag: function (e) {
      let parentRect = document.getElementById('content').getBoundingClientRect()
      let mouseInGrid = false
      if (
        mouseXY.x > parentRect.left &&
        mouseXY.x < parentRect.right &&
        mouseXY.y > parentRect.top &&
        mouseXY.y < parentRect.bottom
      ) {
        mouseInGrid = true
      }
      if (mouseInGrid === true && this.layout.findIndex((item) => item.i === 'drop') === -1) {
        this.layout.push({
          x: (this.layout.length * 2) % (this.colNum || 12),
          y: this.layout.length + (this.colNum || 12), // puts it at the bottom
          w: e.w,
          h: e.h,
          i: 'drop',
        })
      }
      let index = this.layout.findIndex((item) => item.i === 'drop')
      if (index !== -1) {
        // try {
        //   console.log('----', this.layout, index)
        //   this.$refs.gridlayout.$children[this.layout.length].$refs.item.style.display = 'none'
        // } catch {}
        // let el = this.$refs.gridlayout.$children[index]
        // el.dragging = { top: mouseXY.y - parentRect.top, left: mouseXY.x - parentRect.left }
        // let new_pos = el.calcXY(mouseXY.y - parentRect.top, mouseXY.x - parentRect.left)
        let new_pos = { x: 0, y: 0 }
        new_pos.x = Math.floor(((mouseXY.x - parentRect.left) / parentRect.width) * 12)
        new_pos.y = Math.floor(((mouseXY.y - parentRect.top) / parentRect.height) * 12)
        if (mouseInGrid === true) {
          this.$refs.gridlayout.dragEvent('dragstart', 'drop', new_pos.x, new_pos.y, e.w, e.h)
          DragPos.i = String(index)
          DragPos.x = this.layout[index].x
          DragPos.y = this.layout[index].y
        }
        if (mouseInGrid === false) {
          this.$refs.gridlayout.dragEvent('dragend', 'drop', new_pos.x, new_pos.y, e.w, e.h)
          this.layout = this.layout.filter((obj) => obj.i !== 'drop')
        }
      }
    },
    dragend: function (e) {
      let parentRect = document.getElementById('content').getBoundingClientRect()
      let mouseInGrid = false
      if (
        mouseXY.x > parentRect.left &&
        mouseXY.x < parentRect.right &&
        mouseXY.y > parentRect.top &&
        mouseXY.y < parentRect.bottom
      ) {
        mouseInGrid = true
      }
      if (mouseInGrid === true) {
        // alert(`Dropped element props:\n${JSON.stringify(DragPos, ['x', 'y', 'w', 'h'], 2)}`)
        this.$refs.gridlayout.dragEvent('dragend', 'drop', DragPos.x, DragPos.y, e.w, e.h)
        this.layout = this.layout.filter((obj) => obj.i !== 'drop')

        // UNCOMMENT below if you want to add a grid-item

        this.layout.push({
          x: DragPos.x,
          y: DragPos.y,
          w: e.w,
          h: e.h,
          i: DragPos.i,
        })
        // this.layoutUpdatedEvent(this.layout)
        this.$refs.gridLayout.dragEvent('dragend', DragPos.i, DragPos.x, DragPos.y, e.w, e.h)
        // try {
        //   this.$refs.gridLayout.$children[this.layout.length].$refs.item.style.display = 'block'
        // } catch {}
      }
    },
    cancleDrag() {
      this.isEdit = !this.isEdit
      console.log('cancleDrag', this.layout)
      // this.layout = this.layout.filter((obj) => obj.i.indexOf('drop') === -1)
      this.layout = [
        { x: 0, y: 0, w: 2, h: 2, i: '0' },
        { x: 2, y: 0, w: 2, h: 4, i: '1' },
        { x: 4, y: 0, w: 2, h: 5, i: '2' },
        { x: 6, y: 0, w: 2, h: 3, i: '3' },
        { x: 8, y: 0, w: 2, h: 3, i: '4' },
        { x: 10, y: 0, w: 2, h: 3, i: '5' },
        { x: 0, y: 5, w: 2, h: 5, i: '6' },
        { x: 2, y: 5, w: 2, h: 5, i: '7' },
        { x: 4, y: 5, w: 2, h: 5, i: '8' },
      ]
    },
    layoutUpdatedEvent: function (newLayout) {
      console.log('Updated layout: ', newLayout)
      // this.layout = newLayout
      // this.$set(this, 'layout', newLayout)
    },
  },
}
</script>

<style lang="less" scoped>
.container-home__header {
  display: flex;
  justify-content: flex-end;
  button {
    margin: 0 6px;
  }
}
.container-home {
  display: flex;
  .container-home__grids {
    flex: 1;
  }
  .container-home__drags {
    padding: 0 20px;
  }
}
.droppable-element {
  width: 150px;
  text-align: center;
  background: #fdd;
  border: 1px solid black;
  margin: 10px 0;
  padding: 10px;
  // height: 200px;
}

.vue-grid-layout {
  background: #eee;
}

.vue-grid-item:not(.vue-grid-placeholder) {
  background: #ccc;
  border: 1px solid black;
}

.vue-grid-item .resizing {
  opacity: 0.9;
}

.vue-grid-item .static {
  background: #cce;
}

.vue-grid-item .text {
  font-size: 24px;
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  height: 100%;
  width: 100%;
}

.vue-grid-item .no-drag {
  height: 100%;
  width: 100%;
}

.vue-grid-item .minMax {
  font-size: 12px;
}

.vue-grid-item .add {
  cursor: pointer;
}

.vue-draggable-handle {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 0;
  left: 0;
  background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>")
    no-repeat;
  background-position: bottom right;
  padding: 0 8px 8px 0;
  background-repeat: no-repeat;
  background-origin: content-box;
  box-sizing: border-box;
  cursor: pointer;
}

.layoutJSON {
  background: #ddd;
  border: 1px solid black;
  margin-top: 10px;
  padding: 10px;
}

.layoutJSON {
  background: #ddd;
  border: 1px solid black;
  margin-top: 10px;
  padding: 10px;
}

.columns {
  -moz-columns: 120px;
  -webkit-columns: 120px;
  columns: 120px;
}
</style>
